<?php

/**
 * This file is part of the Framework03 PHP framework.
 * @author Walter Tamboer
 * @package Framework03
 */

/**
 * This plugin strips slashes from the server variables.
 */
class FW_Controller_Plugin_DisableMagicQuotes extends FW_Controller_Plugin_Abstract
{
    /**
     * Called before a request is dispatched.
     * @param FW_Controller_Request_Abstract $request
     * @param FW_Controller_Response_Abstract $response
     */
    public function preDispatch(FW_Controller_Request_Abstract $request, FW_Controller_Response_Abstract $response)
    {
        if (get_magic_quotes_gpc())
        {
            $_GET = $this->_stripSlashes($_GET);
            $_POST = $this->_stripSlashes($_POST);
            $_REQUEST = $this->_stripSlashes($_REQUEST);
            $_COOKIE = $this->_stripSlashes($_COOKIE);
        }
    }

    /**
     * Strips the slashes from the given string or array.
     * @param string $value The value to strip the slashes from.
     * @return string
     */
    private function _stripSlashes($value)
    {
        if (is_array($value))
            $value = array_map(array($this, '_stripSlashes'), $value);
        else
            $value = stripslashes($value);

        return $value;
    }
}
